//
//  MNDTPQResultViewController.m
//  minidai
//
//  Created by Vitas on 2018/8/10.
//  Copyright © 2018年 迷你贷. All rights reserved.
//

#import "MNDTPQResultViewController.h"
#import "MNDTPQResultCell.h"

#define kMNDTPQResultCell @"MNDTPQResultCell"

@interface MNDTPQResultViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

//车牌号
@property (weak, nonatomic) IBOutlet UILabel *carNumLabel;
//违章次数
@property (weak, nonatomic) IBOutlet UILabel *rulesNumLabel;
//总扣分
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
//总罚款
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (weak, nonatomic) IBOutlet UISwitch *breakRulesSwitch;

@property(nonatomic, strong) UIView *placeHolderMaskView;

@end

@implementation MNDTPQResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatMaskView];
    [self configContents];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 44;
    // Do any additional setup after loading the view.
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.resultModel.juhedata.list.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MNDBreakRulesResultListModel *model = self.resultModel.juhedata.list[indexPath.row];
    MNDTPQResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MNDTPQResultCell"];
    cell.model = model;
    return cell;
}

- (void)configContents {
    self.carNumLabel.text = self.resultModel.juhedata.hphm;
    self.rulesNumLabel.text = [NSString stringWithFormat:@"%zd",self.resultModel.juhedata.list.count];
    self.scoreLabel.text = self.resultModel.total_fen;
    self.moneyLabel.text = self.resultModel.total_moeny;
    if (self.resultModel.tips.integerValue != 0) {
        self.breakRulesSwitch.on = YES;
    }else {
        self.breakRulesSwitch.on = NO;
    }
    
    if (self.resultModel.juhedata.list.count > 0) {
        self.placeHolderMaskView.hidden = YES;
    }else {
        self.placeHolderMaskView.hidden = NO;
    }
}

#pragma mark - Action

- (IBAction)breakRulesTipAction:(UISwitch *)sender {
    
    [NetworkProgressHUD showHud];
    NSDictionary *params = @{
                             @"break_rule_id":self.resultModel.break_rule_id,
                             @"is_open":sender.on == YES ? @"1" : @"0"
                             };
    [APIManager sendRequestWithURL:API_OPEN_OR_CLOSE_BREAK_RULES parameters:[MNDSign signedParamWithDict:params] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            if (sender.on == YES) {
                [NetworkProgressHUD showHudMsg:@"开启成功"];
            }else {
                 [NetworkProgressHUD showHudMsg:@"关闭成功"];
            }
            
        }else {
            sender.on = sender.on == YES ? NO : YES;
            [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
        }
    } onFailure:^(NSError *error) {
        sender.on = sender.on == YES ? NO : YES;
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

#pragma mark - 无数据时显示图片
- (void)creatMaskView {
    UIView *maskView = [[UIView alloc]initWithFrame:self.tableView.bounds];
    maskView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
    //    [self.view insertSubview:maskView aboveSubview:self.tableView];
    [self.tableView addSubview:maskView];
    UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"financen_detail_data_empty"]];
    UILabel *tipLabel = [[UILabel alloc]init];
    tipLabel.text = @"没有违章记录";
    tipLabel.font = [UIFont systemFontOfSize:14];
    tipLabel.textColor = kColorGray;
    
    [maskView addSubview:tipLabel];
    [maskView addSubview:imgV];
    
    imgV.layer.cornerRadius = imgV.bounds.size.height*0.5;
    imgV.clipsToBounds = YES;
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(maskView);
        make.centerY.equalTo(maskView).mas_offset(-80);
    }];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(imgV);
        make.top.equalTo(imgV.mas_bottom).offset(10);
    }];
    
    self.placeHolderMaskView = maskView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
